package com.yunxun.coinmessage.controller;

import com.yunxun.coinmessage.entity.ClassifiedPacket;
import com.yunxun.coinmessage.service.ClassifiedPacketService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
;


import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClassifiedPacketController {
    @Autowired
    ClassifiedPacketService classifiedPacketService;

    @RequestMapping(value = "/packet-list")
    public String findAll(Model model){
        List<ClassifiedPacket> classifiedPackets=  classifiedPacketService.findAll();
        Integer total=classifiedPacketService.getTotal();
        model.addAttribute("classifiedPackets",classifiedPackets);
        model.addAttribute("total",total);
        return "packet-list";
    }
    @RequestMapping(value = "/openAdd")
    public String openAdd(){
        return "packet-add";
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public String add(ClassifiedPacket classifiedPacket){
        return  classifiedPacketService.add(classifiedPacket);

    }
    @RequestMapping(value = "/deleteClassifiedPacketById")
    @ResponseBody
    public  String deleteClassifiedPacketById(@RequestParam(value = "pid") int pid){
        return  classifiedPacketService.deleteById(pid);

    }
    @RequestMapping(value = "/findById")
    public  String  findById(Integer pid,Model model){
        ClassifiedPacket classifiedPacket=classifiedPacketService.findById(pid);
        model.addAttribute("classifiedPacket",classifiedPacket);
        return "pack-update";
    }
    @RequestMapping(value = "/updateById")
    @ResponseBody
    public String updateById(ClassifiedPacket classifiedPacket){
       return   classifiedPacketService.updateById(classifiedPacket);
    }

    /**
     * 打开更新分类的窗口并回显数据
     * @param pid
     * @param model
     * @return
     */
    @RequestMapping(value = "/openUpdate")
    public String openUpDate(Integer pid,Model model){
        ClassifiedPacket classifiedPacket=classifiedPacketService.findById(pid);
        model.addAttribute("classifiedPacket",classifiedPacket);
        return "packet-update";

    }
}
